package com.seocoo.worker.user.account;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.Const;
import com.seocoo.worker.base.UtilStatus;
import com.seocoo.worker.base.Utils;
import com.seocoo.worker.base.entity.ELogin;
import com.seocoo.worker.base.widget.AppToolBer;
import com.seocoo.worker.base.widget.EditTextCloseView;
import com.seocoo.worker.user.account.entity.EDeposit;
import com.seocoo.worker.user.account.model.DepositModel;
import com.seocoo.worker.user.account.model.req.ReqDepositApply;
import com.seocoo.worker.user.login.model.ReqSMS;
import com.seocoo.worker.user.account.model.CardModel;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;
import com.sfh.lib.utils.ThreadTaskUtils;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/***
 * 提现
 */
public class DepositActivity extends MVVMActivity {
    private AppToolBer appToolBer;
    private LinearLayout llNobank;
    private ConstraintLayout llBank;
    private TextView tvBank;
    private TextView tvNumber;
    private TextView tv_min;
    private EditTextCloseView etMoney;
    private TextView tvCode;
    private EditTextCloseView etCode;

    EDeposit mDeposit;

    public static void start(Context context) {
        Intent intent = new Intent(context, DepositActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_deposit_activity);
        UtilStatus.initStatus(this, Color.WHITE);
        initView();
    }

    private void initView() {
        appToolBer = findViewById(R.id.appToolBer);
        llNobank = findViewById(R.id.ll_nobank);
        llBank = findViewById(R.id.ll_bank);
        tvBank = findViewById(R.id.tv_bank);
        tvNumber = findViewById(R.id.tv_number);
        etMoney = findViewById(R.id.et_money);
        tvCode = findViewById(R.id.tv_code);
        etCode = findViewById(R.id.et_code);
        tv_min = findViewById(R.id.tv_min);

        Utils.setEditTextInputSize(etMoney, 2);

        tvCode.setOnClickListener(v -> {
            //4:提现申请-验证码
            ELogin login = AppCacheManager.getCacheListener().getObject(Const.LOGIN_KEY, ELogin.class);
            getViewModel(CardModel.class).sendSMS(new ReqSMS(login.getUserInfo().getPhoneNumber(), "4"));
        });

        findViewById(R.id.tv_add_bank).setOnClickListener(v -> {
            //添加银行卡
            AddBankCarActivity.start(DepositActivity.this, 1001);
        });

        findViewById(R.id.iv_look_bank).setOnClickListener(v -> {
            //我的银行卡
            MyBankCarActivity.start(DepositActivity.this, 1002);
        });
        findViewById(R.id.tv_all).setOnClickListener(v -> {
            //全部提现
            if (mDeposit != null) {
                etMoney.setText(mDeposit.balanceMoney + "");
            }
        });
        findViewById(R.id.tv_ok).setOnClickListener(v -> {
            if (mDeposit == null) {
                return;
            }
            if (this.mDeposit.bankCard == null || TextUtils.isEmpty(this.mDeposit.bankCard.cardId)) {
                showToast("请绑定提现银行卡!");
                return;
            }
            String money = etMoney.getText().toString().trim();
            if (TextUtils.isEmpty(money)) {
                showToast("请输入提取金额!");
                return;
            }
            if (Float.valueOf(money) < mDeposit.minDepositMoney) {
                showToast("提取金额不能小于最低提现金额!");
                return;
            }
            if (Float.valueOf(money) > mDeposit.balanceMoney) {
                showToast("提取金额不能大于可提现金额!");
                return;
            }

            String code = etCode.getText().toString().trim();

            if (TextUtils.isEmpty(code)) {
                showToast("请输入验证码!");
                return;
            }

            ReqDepositApply apply = new ReqDepositApply();
            apply.cardId = mDeposit.bankCard.cardId;
            apply.depositMoney = money;
            apply.smsCode = code;
            getViewModel(DepositModel.class).depositApply(apply);
        });

        getViewModel(DepositModel.class).queryDeposit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == 1001 || requestCode == 1002) && resultCode == RESULT_OK) {
            getViewModel(DepositModel.class).queryDeposit();
        }
    }

    @LiveDataMatch
    public void onDepositApply() {

        DialogBuilder dialogBuilder = new DialogBuilder();
        dialogBuilder.setMessage("您的提现申请已提交至平台，会尽快处理，请耐心等待!");
        dialogBuilder.setHideCancel(true);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setOkListener((dialog, which) -> {
            dialog.dismiss();
            finish();
        });
        showDialog(dialogBuilder);
    }

    @LiveDataMatch
    public void onDepositSuccess(EDeposit eDeposit) {
        this.mDeposit = eDeposit;
        if (this.mDeposit != null) {
            if (this.mDeposit.bankCard == null || TextUtils.isEmpty(this.mDeposit.bankCard.cardId)) {
                //没有银行卡
                llNobank.setVisibility(View.VISIBLE);
                llBank.setVisibility(View.GONE);

            } else {
                llNobank.setVisibility(View.GONE);
                llBank.setVisibility(View.VISIBLE);
                tvBank.setText(this.mDeposit.bankCard.bankName);
                tvNumber.setText("尾号" + this.mDeposit.bankCard.cardNo.substring(this.mDeposit.bankCard.cardNo.length() - 4) + "储蓄卡");
            }

            tv_min.setText("最低提现金额：" + this.mDeposit.minDepositMoney + "元，可提现金额：" + this.mDeposit.balanceMoney + "元,");
        }
    }


    private int sizeCount;
    private Future sizeFuture;

    @LiveDataMatch
    public void onSendCodeSuccess() {
        //发验证码
        sizeCount = 60;
        if (sizeFuture != null && !sizeFuture.isCancelled()) {
            sizeFuture.cancel(true);
            sizeFuture = null;
        }
        sizeFuture = ThreadTaskUtils.scheduleWithFixedDelay(() -> {
            runOnUiThread(() -> {
                sizeCount--;
                if (sizeCount > 0) {
                    tvCode.setEnabled(false);
                    tvCode.setText(String.format("%sS", sizeCount));

                } else {
                    sizeFuture.cancel(true);
                    tvCode.setEnabled(true);
                    tvCode.setText("获取验证码");
                }
            });
        }, 1000, 1000, TimeUnit.MILLISECONDS);
        this.putFuture(sizeFuture);

    }
}
